package com.android.phoenix;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.android.phoenix.database.BycecleDataBaseHelper;
import com.android.phoenix.util.ICacheUtil;
import com.baidu.mapapi.SDKInitializer;
import com.dfzy.android.model.CommonApplication;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public class MainApplication extends CommonApplication implements ICacheUtil {
	private List<Finishable> finishableList = new ArrayList<Finishable>();

	// wanchao
	private static MainApplication mApplicationInstance = null;
	public static SharedPreferences mSharedPreferences;
	
	//wanglei
	public static String DATA_DIR;
	public static String CACHE_DIR;
	public static String RECORD_CACHE_DIR;
	
	public static final String PACKAGE_NAME = "com.bicycle";

	/** 数据库连接类 */
	private static BycecleDataBaseHelper mBycecleDataBaseHelper = null;

	@Override
	public void exit() {
		for (Finishable f : finishableList) {
			f.exit();
		}
	}

	@Override
	public void putExitList(Finishable f) {
		finishableList.add(f);
	}

	@Override
	public void removeFromExitList(Finishable f) {
		finishableList.remove(f);
	}

	@Override
	public void init() {

	}

	/**
	 * 地图定位需要首先初始化
	 */
	@Override
	public void onCreate() {
		mApplicationInstance = this;
		mSharedPreferences = getSharedPreferences(Constant.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		
		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(this);
		createDirs();
		super.onCreate();
	}

	/**
	 * @Title: getApplicationInstance
	 * @Description: 取得application实例
	 * @author: WanChao
	 * @date: 2015-1-7 下午9:41:38
	 * @return
	 */
	public static MainApplication getApplicationInstance() {
		return mApplicationInstance;
	}

	/**
	 * @Title: getDataHelper
	 * @Description: 取得数据库对象
	 * @author: WanChao
	 * @date: 2015-1-18 下午4:32:53
	 * @param context
	 * @return
	 */
	public static synchronized BycecleDataBaseHelper getDataHelper(
			Context context) {
		if (mBycecleDataBaseHelper == null) {
			mBycecleDataBaseHelper = OpenHelperManager.getHelper(context,
					BycecleDataBaseHelper.class);
		}
		return mBycecleDataBaseHelper;
	}

	/**
	 * @Title: destroyDataHelper
	 * @Description: 销毁数据库
	 * @author: WanChao
	 * @date: 2015-1-18 下午4:33:37
	 */
	public static void destroyDataHelper() {
		if (null != mBycecleDataBaseHelper) {
			OpenHelperManager.releaseHelper();
			mBycecleDataBaseHelper = null;
		}
	}

	public void createDirs() {
		/**
		 * 判断SDcard是否存在，然后觉得将文件存放到sbcard还是手机内存
		 * */
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			DATA_DIR = Environment.getExternalStorageDirectory().getPath()
					+ "/";
		} else {
			DATA_DIR = "data/data/" + PACKAGE_NAME + "/";
		}

		CACHE_DIR = DATA_DIR + "bicycle/";
		RECORD_CACHE_DIR = MainApplication.CACHE_DIR + "record/";


		CACHE_DIR.trim();
		File file1 = new File(CACHE_DIR);
		if (!file1.exists())
			file1.mkdirs();
		RECORD_CACHE_DIR.trim();
		File file2 = new File(RECORD_CACHE_DIR);
		if (!file2.exists())
			file2.mkdirs();
	}
}
